package de.bazinga.rotw.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;

import com.jme.bounding.BoundingBox;
import com.jme.scene.Node;
import com.jme.util.export.binary.BinaryImporter;
import com.jme.util.resource.ResourceLocatorTool;
import com.jme.util.resource.SimpleResourceLocator;
import com.jmex.model.converters.AseToJme;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.Md2ToJme;
import com.jmex.model.converters.Md3ToJme;
import com.jmex.model.converters.MilkToJme;
import com.jmex.model.converters.ObjToJme;

public class ModelCache {

	private static ModelCache instance = new ModelCache();
	
	public static HashMap<String,Node> nodes = new HashMap<String,Node>();
	
	public ModelCache() {}
	
	public void addModel(URL name,Node node) {
		nodes.put(name.getFile(), node);
	}
	
	public Node getModel(URL name) {
		if(nodes.containsKey(name.getFile())) {
			return (Node)nodes.get(name.getFile());
		}
		
		try{
			Node test = (Node) BinaryImporter.getInstance().load(name);

			nodes.put(name.getFile(), test);
		}catch(IOException e) {
			System.out.println("Fuck");
			System.exit(0);
		}
		return (Node)nodes.get(name.getFile());
	}
	
	
	public Node getModel(String name) {
		if(nodes.containsKey(name)) {
			return (Node)nodes.get(name);
		}
		return null;
	}
	
	
	public boolean containsModel(String name) {
		return nodes.containsKey(name);
	}
	
	
	public Node addModel(URL texture, URL model, String modelFile) {
        Node zombie = loadModel(texture, model, modelFile);
        nodes.put(modelFile, zombie);
        
        return zombie;
	}
	
	
	public Node loadModel(URL texture, URL model, String modelFile) {
		try {
            ResourceLocatorTool.addResourceLocator(
                    ResourceLocatorTool.TYPE_TEXTURE, 
                    new SimpleResourceLocator(texture));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        try {
            ResourceLocatorTool.addResourceLocator(
                    ResourceLocatorTool.TYPE_MODEL, 
                    new SimpleResourceLocator(model));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        
        FormatConverter converter = null;
        
        String modelFormat = modelFile.substring(modelFile.lastIndexOf(".") + 1, modelFile.length()).toLowerCase();
        if (modelFormat.equals("3ds")){
        	converter = new MaxToJme();
		} else if (modelFormat.equals("md2")){
			converter = new Md2ToJme();
		} else if (modelFormat.equals("md3")){
			converter = new Md3ToJme();
		} else if (modelFormat.equals("ms3d")){
			converter = new MilkToJme();
		} else if (modelFormat.equals("ase")){
			converter = new AseToJme();
		} else if (modelFormat.equals("obj")){
			converter = new ObjToJme();
		}
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        URL modelUrl = null;
        modelUrl = ResourceLocatorTool.locateResource(ResourceLocatorTool.TYPE_MODEL, modelFile);
        
        Node zombie = null;
        try {
            converter.convert(modelUrl.openStream(), out);
            zombie = (Node) BinaryImporter.getInstance().load(new ByteArrayInputStream(out.toByteArray()));
            zombie.setModelBound(new BoundingBox());
            zombie.updateModelBound();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return zombie;
	}
	
	
	
	
	public static ModelCache getInstance() {
		return instance;
	}
}
